package com.sibco.client.business;

import java.text.SimpleDateFormat;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.sibco.client.system.User;
import com.sibco.client.common.CommUtil;

/**
 * LaborContractListener
 */
public class LaborContractListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset) throws Exception {
	  DoradoContext context = DoradoContext.getContext();
	  User user = (User) context.getAttribute(context.SESSION, "User");
	  String className = dataset.parameters().getString("className");
	  SqlDataset ds = (SqlDataset) dataset;
	  String sql="";
	  //System.out.println("CCCCCCCCCCCCCCCCCCCCCCC:"+dataset.parameters().getString("general_query"));
	  if(className!=null && className.equals("datasetLaborContract")){
		   sql="select lc.*,eoop.employee_type,e.employee_code,e.employee_name,e.id_card_num,sc.COMPANY_CHINESE_NAME," 
		   	  +"(select real_name from clt_pvd_user u where u.user_id=cs.cs_man)cs_man_name " 
		   	  +"from clt_cs_labor_contract lc,clt_pb_employee e,clt_cs_employee_on_off_post eoop,clt_pb_company sc,clt_cs_contract_send cs where " 
	  		  +"cs.contract_send_id=eoop.contract_send_id and sc.company_id=eoop.company_id and e.employee_id=eoop.employee_id "
	  		  +"and eoop.employee_on_off_id=lc.employee_on_off_id and lc.status in(1,2,4) "
	  		  +"and to_char(lc.start_date,'yyyymmdd')<=to_char(sysdate,'yyyymmdd') "
	  		  +"and (lc.end_date is null or to_char(lc.end_date,'yyyymmdd')>=to_char(sysdate,'yyyymmdd')) "
	  		  +"and (eoop.off_post_date is null or to_char(eoop.off_post_date,'yyyymmdd')>=to_char(sysdate,'yyyymmdd')) ";
		  		
		   if((ds.parameters().getString("DATE_COL")==null ||ds.parameters().getString("DATE_COL").equals(""))&&
				   (ds.parameters().getDate("COL_DATE_END")==null ||ds.parameters().getDate("COL_DATE_END").equals("")))
		   {;}else{
			   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			   sql+=" and lc."+ds.parameters().getString("DATE_COL")+" >"+
			   "to_date('" + df.format(ds.parameters().getDate("COL_DATE_END"))+ "','yyyy-MM-dd')";
		   }
		   sql += CommUtil.getRightSql(ds, user, "eoop", "e");
		   sql += CommUtil.setConditionSQL(ds);		   
		   System.out.println("CCCCCCCCCCCCCCCCCCCCCCC:"+sql);
	  }
	  ds.setSql(sql);
      return true;
  }

  public void afterLoadData(Dataset dataset) throws Exception {

  }

}
